using Pinwheel.Toolkits;

namespace Pinwheel.LSerialize.Formatters
{
    [Preserve]
    public sealed unsafe class NullableBuiltinFormatter<T> : LFormatter<T?> where T : unmanaged
    {
        [Preserve]
        public override void Serialize(LSerializeContext* context, LStreamWriter* writer, ref T? value)
        {
            Profiler.Start("NullableBuiltinFormatter.Serialize");
            writer->WriteNumber(context, ref value);
            Profiler.End();
        }

        [Preserve]
        public override void Deserialize(LSerializeContext* context, LStreamReader* reader, ref T? value)
        {
            Profiler.Start("NullableBuiltinFormatter.Deserialize");
            reader->ReadNumber(context, ref value);
            Profiler.End();
        }
    }
    
    [Preserve]
    public sealed unsafe class NullableUnmanagedFormatter<T> : LFormatter<T?> where T : unmanaged
    {
        [Preserve]
        public override void Serialize(LSerializeContext* context, LStreamWriter* writer, ref T? value)
        {
            Profiler.Start("NullableUnmanagedFormatter.Serialize");
            writer->WriteUnmanagedStruct(context, ref value);
            Profiler.End();
        }

        [Preserve]
        public override void Deserialize(LSerializeContext* context, LStreamReader* reader, ref T? value)
        {
            Profiler.Start("NullableUnmanagedFormatter.Deserialize");
            reader->ReadUnmanagedStruct(context, ref value);
            Profiler.End();
        }
    } 
}

